10 ' merlinms.bas - FreeWare 2005 - eric tchong
20 GOTO 80 ' begin
30 SAVE"merlinms",A:LIST-80
40 GOTO 380 ' clear line
50 GOTO 400 ' get key
60 GOTO 610 ' color combinations
70 ' begin
80 CLS:KEY OFF:DEFDBL E,M,N,S,R:DIM MN(16), SN(16):DEFSTR Q:Q=MKI$(0)
90 FOR X=1 TO 16:READ MN(X):NEXT
100 PRINT "Merlin Magic Square Generator":PRINT
110 PRINT "Save results in an ascii diskfile <y/n> ?"
120 GOSUB 50:CLS
130 IF ASC(Q)=89 OR ASC(Q)=121 THEN COPY=1 ELSE COPY=0:GOTO 160
140 LINE INPUT "Enter a DOS filename ? ";Z$
150 OPEN "O",#1,Z$:CLS
160 INPUT "Number 41 to 99999999 [0=exit] ";N
170 IF N=0 THEN CLS:END
180 IF N<41 OR N>99999999# THEN GOSUB 40:GOTO 160
190 CLS:N$=STR$(N):L=LEN(N$)-1
200 S=N-30:E=INT(S/4):R=S-4*E
210 FOR X=1 TO 16
220  IF X=3 OR X=5 OR X=10 OR X=16 THEN SN(X)=MN(X)+E+R ELSE SN(X)=MN(X)+E
230 NEXT
240 PRINT "Merlin magic square of";N:PRINT
250 IF COPY THEN PRINT #1,"Merlin magic square of";N:PRINT #1,""
260 ' adjust printing
270 FOR X=1 TO 16
280  IF L=2 THEN PRINT USING "###";SN(X);:IF COPY THEN PRINT #1,USING "###";SN(X);:GOTO 350 ELSE 350
290  IF L=3 THEN PRINT USING "####";SN(X);:IF COPY THEN PRINT #1,USING "####";SN(X);:GOTO 350 ELSE 350
300  IF L=4 THEN PRINT USING "#####";SN(X);:IF COPY THEN PRINT #1,USING "#####";SN(X);:GOTO 350 ELSE 350
310  IF L=5 THEN PRINT USING "######";SN(X);:IF COPY THEN PRINT #1,USING "######";SN(X);:GOTO 350 ELSE 350
320  IF L=6 THEN PRINT USING "#######";SN(X);:IF COPY THEN PRINT #1,USING "#######";SN(X);:GOTO 350 ELSE 350
330  IF L=7 THEN PRINT USING "########";SN(X);:IF COPY THEN PRINT #1,USING "########";SN(X);:GOTO 350 ELSE 350
340  PRINT USING "##########";SN(X);:IF COPY THEN PRINT #1,USING "##########";SN(X);
350  IF X/4=INT(X/4) THEN PRINT:PRINT:IF COPY THEN PRINT #1,"":PRINT #1,""
360 NEXT:GOTO 480
370 ' clear line
380 PRINT CHR$(30);:PRINT STRING$(79,32):PRINT CHR$(30);:RETURN
390 ' get key
400 LSET Q=MKI$(0)
410 WHILE CVI(Q)=0:MID$(Q,1)=INKEY$:WEND:RETURN
420 ' Merlin magic square 4x4
430 DATA  7, 10, 13,  0
440 DATA 12,  1,  6, 11
450 DATA  2, 15,  8,  5
460 DATA  9,  4,  3, 14
470 ' Show all combinations in color
480 PRINT "Magic Square Combinations":PRINT
490 FOR X=1 TO 16
500  IF L=2 THEN GOSUB 610:PRINT USING "###";SN(X);:GOTO 570
510  IF L=3 THEN GOSUB 610:PRINT USING "####";SN(X);:GOTO 570
520  IF L=4 THEN GOSUB 610:PRINT USING "#####";SN(X);:GOTO 570
530  IF L=5 THEN GOSUB 610:PRINT USING "######";SN(X);:GOTO 570
540  IF L=6 THEN GOSUB 610:PRINT USING "#######";SN(X);:GOTO 570
550  IF L=7 THEN GOSUB 610:PRINT USING "########";SN(X);:GOTO 570
560  GOSUB 610:PRINT USING "##########";SN(X);
570  IF X/4=INT(X/4) THEN PRINT:PRINT
580 NEXT
590 COLOR 7,0:GOTO 160
600 ' Color subroutine
610 IF X=1 OR X=4 OR X=13 OR X=16 THEN COLOR 15,0:RETURN ' white
620 IF X=2 OR X=3 OR X=14 OR X=15 THEN COLOR 11,0:RETURN ' cyan
630 IF X=6 OR X=7 OR X=10 OR X=11 THEN COLOR 14,0:RETURN ' yellow
640 IF X=5 OR X=8 OR X=9  OR X=12 THEN COLOR 12,0:RETURN ' red
650 RETURN
